﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace FootBallMatchProject.Piography.Admin
{
    public partial class AddEditLeague : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataClassLayer.League league = new DataClassLayer.League();
            DataClassLayer.LeagueCollection leagues = new DataClassLayer.LeagueCollection();
            if (!IsPostBack)
            {
                if (Request.QueryString["operation"] != null &&
                    Request.QueryString["operation"].ToString() == "Edit")
                {
                    
                    if (Request.QueryString["id"] != null &&
                    Request.QueryString["id"].ToString() != "")
                    {
                        league.ID = int.Parse(Request.QueryString["id"].ToString());
                        DataTable table = leagues.FillList(league);
                        if (table.Rows.Count == 1)
                        {
                            btnClear.Visible = false;
                            txtLeague.Text = table.Rows[0]["Name"].ToString();
                            txtGeneralInformation.Text = table.Rows[0]["Description"].ToString();
                            LeagueLogo.ImageUrl = "~/images/League_Logos/" + Request.QueryString["id"].ToString() + "/" + table.Rows[0]["Logo"].ToString();
                            LeagueLogo.Visible = true;
                            hfLeagueLogo.Value = table.Rows[0]["Logo"].ToString();
                        }
                    }
                }
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("LeagueList.aspx");
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataClassLayer.League league = new DataClassLayer.League();
            DataClassLayer.LeagueCollection leagues = new DataClassLayer.LeagueCollection();
            league.Name = txtLeague.Text;
            league.Logo = hfLeagueLogo.Value;
            league.Description = txtGeneralInformation.Text;
            if (Request.QueryString["operation"] != null &&
                Request.QueryString["operation"].ToString() == "Edit")
            {
                if (Request.QueryString["id"] != null &&
                              Request.QueryString["id"].ToString() != "")
                {
                    league.ID = int.Parse(Request.QueryString["id"].ToString());
                    bool chkvalue = leagues.Edit(league);
                    if (chkvalue)
                    {
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/images/League_Logos/" + GetID())))
                        {
                            System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/images/League_Logos/" + GetID()));
                        }
                        string fileName = null;
                        string[] fileArray = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/League_Logos"));
                        for (int i = 0; i < fileArray.Length; i++)
                        {
                            fileName = Path.GetFileName(fileArray[i]);
                            File.Copy(fileArray[i], HttpContext.Current.Server.MapPath("~/images/League_Logos/" + GetID() + "/" + fileName), true);
                        }
                        Response.Redirect("LeagueList.aspx");
                    }
                    else
                    {
                        ///pop msg
                    }
                }
                else
                {
                    ///pop msg
                }
            }
            else
            {
                bool chkvalue = leagues.Add(league);
                if (chkvalue)
                {
                    if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/images/League_Logos/" + GetID())))
                    {
                        System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/images/League_Logos/" + GetID()));
                    }
                    string fileName = null;
                    string[] fileArray = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/images/Temp/League_Logos"));
                    for (int i = 0; i < fileArray.Length; i++)
                    {
                        fileName = Path.GetFileName(fileArray[i]);
                        File.Copy(fileArray[i], HttpContext.Current.Server.MapPath("~/images/League_Logos/" + GetID() + "/" + fileName), true);
                    }
                    Response.Redirect("LeagueList.aspx");
                }
                else
                {
                    ///pop msg
                }
            }
        }

        protected void btnUploadLeagueLogo_Click(object sender, EventArgs e)
        {
            string Thump_Name_File = "";
            DataAccessObject.StaticClass.UploadData(ref LeagueLogo, "~/images/temp/League_Logos/", UploadLeagueLogo, ref Thump_Name_File);
            hfLeagueLogo.Value = Thump_Name_File;
            LeagueLogo.Visible = true;
        }
        private string GetID()
        {
            if (Request.QueryString["operation"] != null &&
                Request.QueryString["operation"].ToString() == "Edit")
            {
                if (Request.QueryString["id"] != null &&
                Request.QueryString["id"].ToString() != "")
                {
                    return Request.QueryString["id"].ToString();
                }
                else
                    return "";
            }
            else
            {
                DataClassLayer.League league = new DataClassLayer.League();
                DataClassLayer.LeagueCollection leagues = new DataClassLayer.LeagueCollection();
                return leagues.MaxID().ToString();
            }
        }
    }
}